<template>
  <a-modal
    title="新增供应商联系人"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="供应商资料名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select
            placeholder="请输入供应商资料名称"
            v-decorator="['suppDataId',{rules: [{ required: true, message: '请输入供应商资料名称！'}]}]">
            <a-select-option
              v-for="(item,index) in suppDataList"
              :key="index"
              :value="item.id">{{ item.suppDataName }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="联系人"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入联系人" v-decorator="['suppPerson', {rules: [{required: true, message: '请输入联系人！'}]}]" />
        </a-form-item>
        <a-form-item
          label="联系电话"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input
            placeholder="请输入联系电话"
            allowClear
            :maxLength="11"
            v-decorator="['suppPhone', {rules: [{ required: true,validator: validatorPhone }]}]"
          />
        </a-form-item>
        <a-form-item
          label="主联系人"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-radio-group
            placeholder="是否为主联系人"
            v-decorator="['chiefSuppPerson']">
            <a-radio v-for="(item,index) in chiefSuppPersonData" :key="index" :value="item.code">{{
              item.name
            }}
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          label="备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-textarea placeholder="请输入备注" v-decorator="['remark']" :auto-size="{ minRows: 3, maxRows: 6 }"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { suppPersonAdd } from '@/api/modular/main/suppperson/suppPersonManage'
import { suppDataList } from '@/api/modular/main/suppdata/suppDataManage'

export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        chiefSuppPersonData: [],
        suppDataList: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      async add (record) {
        this.visible = true
        await suppDataList().then(res => {
          this.suppDataList = res.data
        })
        this.chiefSuppPersonData = [
          {
            name: '是',
            code: 1
          },
          {
            name: '否',
            code: -1
          }
        ]
        // 设置初始值
        setTimeout(() => {
          this.form.setFieldsValue({
            suppDataId: this.suppDataList.length !== 0 ? this.suppDataList[0].id : '',
            chiefSuppPerson: 1
          })
        }, 100)
      },
      /**
       * 手机号码校验
       */
      validatorPhone (rule, value) {
        const phoneReg = /^1[3|4|5|7|8][0-9]{9}$/
        if (value === undefined) {
          return Promise.reject(new Error('请输入电话号码！'))
        } else {
          if (!phoneReg.test(value)) {
            return Promise.reject(new Error('请输入正确格式的电话号码！'))
          }
          return Promise.resolve()
        }
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            suppPersonAdd(values).then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error(res.message)// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
